package com.HospitalOutpatient.service.impl;

import com.HospitalOutpatient.dao.DiagnosisDao;
import com.HospitalOutpatient.dao.MedicationDao;
import com.HospitalOutpatient.dao.ReceptionDao;
import com.HospitalOutpatient.dto.DiagnosisDto;
import com.HospitalOutpatient.entity.Diagnosis;
import com.HospitalOutpatient.entity.Medication;
import com.HospitalOutpatient.entity.Reception;
import com.HospitalOutpatient.service.DiagnosisService;
import com.HospitalOutpatient.vo.DiagnosisVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("diagnosisService")
public class DiagnosisServiceImpl implements DiagnosisService {

    @Autowired
    private DiagnosisDao diagnosisDao;
    @Autowired
    private MedicationDao medicationDao;

    @Autowired
    private ReceptionDao receptionDao;

    @Override
    public boolean addDiagnosis(DiagnosisDto diagnosisDto) {

        Diagnosis diagnosis = new Diagnosis();

        List<String> medicationIdList = diagnosisDto.getMedicationIds();
        String medicationIds = new String();
        Integer totalCharges = new Integer(0);
        for (String s : medicationIdList) {
            medicationIds += s+",";
            Medication medication = medicationDao.selectMedicationById(s);

            totalCharges += Integer.parseInt(medication.getCharge());
        }
        if(medicationIds.length() != 0){
            medicationIds = medicationIds.substring(0,medicationIds.length()-1);
        }

        diagnosis.setNarration(diagnosisDto.getNarration());
        diagnosis.setDiagnosisResult(diagnosisDto.getDiagnosisResult());
        diagnosis.setMedicationIds(medicationIds);
        diagnosis.setTotalCharges(String.valueOf(totalCharges));

        int i = diagnosisDao.addDiagnosisDao(diagnosis);
        if(i != 1){
            return false;
        }
        Reception reception = receptionDao.selectReceptionById(diagnosisDto.getReceptionId());
        reception.setDiagnosisId(diagnosis.getDiagnosisId());

        if(receptionDao.updateDiagnosisToReception(reception) !=1){
            return false;
        }

        return true;
    }

    @Override
    public DiagnosisVo getDiagnosisByReceptionId(String receptionId) {
        Reception reception = receptionDao.selectReceptionById(receptionId);
        String diagnosisId = reception.getDiagnosisId();
        if(diagnosisId==null){
            return null;
        }
        Diagnosis diagnosis = diagnosisDao.selectDiagnosisById(diagnosisId);

        DiagnosisVo diagnosisVo = new DiagnosisVo();

        diagnosisVo.setNarration(diagnosis.getNarration());
        diagnosisVo.setDiagnosisResult(diagnosis.getDiagnosisResult());
        diagnosisVo.setTotalCharges(diagnosis.getTotalCharges());

        List<Medication> medicationList = new ArrayList<>();

        String medicationIds = diagnosis.getMedicationIds();
        String[] medicationIdArray = medicationIds.split(",");
        for (String s : medicationIdArray) {
            Medication medication = medicationDao.selectMedicationById(s);
            medicationList.add(medication);
        }
        if(medicationList.size() == 0){
            medicationList = null;
        }
        diagnosisVo.setMedicationList(medicationList);


        return diagnosisVo;
    }
}
